math *900

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    [l, r] = list(map(int, input().split()))
    a = (r-l+1)//2
    res = (2*(l%2)-1)*a
    if (r-l+1)%2==1:
        res += r*((-1)**r)
    print(res)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  long long l, r;
  while(t--){

    cin>>l>>r;
    long long odd_l, odd_r;
    l--;
    if(l % 2 == 0){
      odd_l = l/2 * l/2;
    }
    else{
      odd_l = (l/2 + 1) * (l/2 + 1);
    }
    long long even_l = l/2 * (l/2 + 1);
    if(r % 2 == 0){
      odd_r = r/2 * r/2;
    }
    else{
      odd_r = (r/2 + 1) * (r/2 + 1);
    }
    long long even_r = r/2 * (r/2 + 1);

    long long even_s = even_r - even_l;
    long long odd_s = odd_r - odd_l;

    cout<<(even_s - odd_s)<<"\n";
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords